<template>

	<view class="" @touchmove.prevent.stop>

		<!-- 购物车列表 -->
		<view class="shopCarBox" :style="{bottom:`${152 + footerB}rpx`}" v-if="shopCarShow" @click="changeShopCarShow">
			<view class="shopCarContent" @click.stop>
				<scroll-view class="foodList" scroll-y="true"	>
					<!-- @scrolltolower="lowerShopCar" -->
					<!-- <checkbox value="index" :checked="foodItem.checked" ref="childCheckBox" /> -->
					<view class="foodItem" v-for="(foodItem, foodIndex) in shopCarInfo.shopCarList"
					:key="foodIndex">
						<image class="foodImage" :src="foodItem.foodImage" mode="aspectFill"></image>
						<view class="">
							<view class="">{{ foodItem.foodName }}</view>
							<view class="specifications" v-if="foodItem.specifications.length">
								规格：<template v-for="(item,index) in foodItem.specifications">{{item.name}}
									<template v-if="index != foodItem.specifications.length-1">-</template>
								</template>
							</view>
							<view class="">
								<view class="foodPrice">
									￥{{accMul(foodItem.showPrice, foodItem.count)}}
									<!-- ￥{{foodItem.totalPrice}} -->
								</view>
								<view class="showInput">
									<!-- <button class="down"
										@click="changeShopNum('down',  foodItem, foodItem,siteAddrId)">
									</button> -->
									<view class="operation"  v-if="foodItem.count" @click="changeShopNum('down',  foodItem, foodItem,siteAddrId)">
										<image src="/static/minus.jpg"></image>
									</view>
									<text class="count"  v-if="foodItem.count">{{foodItem.count}}</text>
									<view class="operation" @click="changeShopNum('add',  foodItem, foodItem,siteAddrId)">
										<image src="/static/add.png"></image>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<empty text="没有购物车数据,快去挑选商品吧" v-if="shopCarInfo.shopCarList.length == 0"></empty>
			</view>
		</view>

		<!-- 购物车底部 -->
		<view class="shopCarBottom" :style="{bottom:footerB+'rpx'}">
			<view class="shopCarBottomLeft" @click="changeShopCarShow">
				<view class="">
					<image src="/static/shopCar.png"></image>
					<div class="num" v-if="shopCarInfo.total > 0">{{ shopCarInfo.total }}</div>
				</view>
				<view class="">￥{{ shopCarInfo.totalPrice ? shopCarInfo.totalPrice : 0 }}</view>
			</view>
			<!-- @click="goToPage('createOrder')" -->
			<view :class=" { dis: shopCarInfo.total <= 0,shopCarBottomRight:true}" @click="checkShopCarInfo">去结算</view>
		</view>
	</view>
</template>

<script>
	import util from "@/utils/util.js";
	import MyMinix from '@/utils/minix/minix.js';
	import {
		queryShoppingCart
	} from "@/request/api.js"
	export default {
		name: "shopCar",
		mixins: [MyMinix],
		props: {
			siteAddrId: {
				type: String,
				default: ""
			},
			footerB: {
				type: Number,
				default: 0
			},
			mustPurchaseShowToast:{
				type:Boolean,
				default:true
			},
			
			// 120rpx
		},
		data() {
			return {
				shopCarInfo: '', //购物车列表数据
				shopCarShow: false, //购物车的显隐
				pageNo:1,
				pageTotal:0,
			};
		},
		mounted(){
			// this.getShopCar();
		},
		methods: {

			//获取购物车列表
			async getShopCar() {
				let that = this;
				let userInfo = uni.getStorageSync('userInfo');
				let res = await queryShoppingCart({
					method: "GET",
					data: {
						menuId:userInfo.menuId,
						phone:userInfo.phoneNumber,
						pageNo: this.pageNo,
						pageSize: 100
					}
				});
				let shoppingCart = res.data;
				this.setShopCarInfo(shoppingCart);
				// this.pageTotal = res.result.pages;
				this.$store.commit("setShopCarInfo", this.shopCarInfo);
			},
			getVuexShopCarInfo(){
				let shopCarInfo =  this.$store.getters.getShopCarInfo;
				this.setShopCarInfo(shopCarInfo.shopCarList);
			},
			setShopCarInfo(shoppingCart){
				let total = 0,
					totalPrice = 0;
				shoppingCart.forEach(item => {
					total = total + item.count;
					// totalPrice = this.accAdd(totalPrice, this.accMul(item.foodPrice, item
					// 	.count));
					totalPrice = this.accAdd(totalPrice, this.accMul(item.showPrice, item.count));
				});
				this.shopCarInfo = {
					total,
					shopCarList: shoppingCart,
					totalPrice
				};
			},
			//展示购物车列表
			changeShopCarShow() {
				this.shopCarShow = !this.shopCarShow;
			},
			// 判断店铺是否歇业
			isShopRest() {
				// let shopBusinessTime = uni.getStorageSync('shopBusinessTime');
				let datas = this.getStorePage();
				let shopBusinessTime = datas['shopBusinessTime']
				let nowDate = new Date();
				let year = nowDate.getFullYear();
				let month = nowDate.getMonth() + 1;
				let day = nowDate.getDate();
				let dateWithFront = `${year}/${month}/${day}`
				let nowTemp = nowDate.getTime();
				let shopRestItem = null;
				this.shopCarInfo.shopCarList.some(item => {
					return shopBusinessTime.map(items => {
						if (item.storeId == items.storeId) {
							let businessTime = items.businessTime.split('~');
							items.startTimeTemp = new Date(dateWithFront + ' ' + businessTime[0]).getTime();
							items.endTimeTemp = new Date(dateWithFront + ' ' + businessTime[1]).getTime();
							// 如果开始时间比结束时间大 
							if (items.startTimeTemp > items.endTimeTemp) {
								if (nowTemp < items.startTimeTemp) {
									items.startTimeTemp = parseInt(items.endTimeTemp) - 1000 *
										60 * 60 * 24;
								} else if (nowTemp > items.endTimeTemp) {
									items.endTimeTemp = parseInt(items.endTimeTemp) + 1000 * 60 *
										60 * 24;
								}
							}
							if (nowTemp > items.startTimeTemp && nowTemp < items.endTimeTemp) {
								// console.log(items, "营业中");
							} else {
								// console.log(items, "歇业了");
								shopRestItem = items;
								return true;
							}
						}
					})
				})
				return shopRestItem;
			},
			lowerShopCar(){
				if(this.pageTotal > this.pageNo){
					this.pageNo = this.pageNo + 1;
					this.getShopCar();
				}
			},
			
			// 去结算
			async checkShopCarInfo() {
				console.log(this.shopCarInfo,'this.shopCarInfo');
				if (!this.shopCarInfo || this.shopCarInfo?.total <= 0) {
					uni.showToast({
						title: '请添加菜品！',
						icon: 'none'
					})
					return;
				}
				
				
				let shopRestItem =  this.isShopRest();
				if(shopRestItem){
					uni.showModal({
						title: "温馨提示",
						content: `${shopRestItem.storeName}店铺歇业中,该店铺的营业时间是${shopRestItem.businessTime}`,
						confirmText: "确定",
						showCancel: false,
						success: (res) => {
							
						}
					})
					return;
				}
				// let mustPurchaseItem = util.isMustPurchase(this.shopCarInfo.shopCarList);
				// if(mustPurchaseItem){
				// 	if(this.mustPurchaseShowToast){
				// 		uni.showToast({
				// 			title: `${mustPurchaseItem.storeName}店铺没有选择必点菜品！`,
				// 			icon: 'none',
				// 			duration:3000
				// 		})
				// 	}
				// 	this.$emit("setMustPurchase",mustPurchaseItem)
				// 	return;
				// }
				
				let token = uni.getStorageSync('token');
				// 如果没有登录 显示登录弹窗
				if (!token) {
					this.auth = true;
					return;
				}
				// 跳转结算页面  addressId=${this.siteAddrId}&
				util.goToUrl(`/pages/order/createOrder?type=createOrder`)
			},

		}
	}
</script>

<style lang="scss" scoped>
	// 购物车底部
	.shopCarBottom {
		z-index: 2;
		position: fixed;
		// left: 0;
		bottom: 0;
		width: 100%;
		height: 152rpx;
		margin: 0 auto;
		background-color: #fff;
		box-shadow: 5px 0px 5px rgba(0, 0, 0, 0.05);
		@include flexs(space-between, center);
		// padding: 0 32rpx;

		image {
			width: 64rpx;
			height: 64rpx;
		}
		.shopCarBottomLeft{
			padding:18rpx 32rpx;
			height:calc(100% - 36rpx);
		}
		.shopCarBottomRight{
			margin:18rpx 32rpx 18rpx 0;
		}

		>view {
			&:nth-child(1) {
				>view {
					position: relative;

					>.num {
						position: absolute;
						right: -5px;
						top: -5px;
						font-size: 20rpx;
						width: 30rpx;
						height: 30rpx;
						border-radius: 50%;
						text-align: center;
						line-height: 30rpx;
						color: #fff;
						background-color: #FF3C3C;
					}
				}

				>view:nth-child(2) {
					margin-left: 20rpx;
					font-weight: bold;
					font-size: 32rpx;
					color:#FF0404;
				}
			}

			@include flexs();

			&:nth-child(2) {
				background-color:#EB5B1E;
				color: #fff;
				font-size: 32rpx;
				width: 180rpx;
				height: 72rpx;
				line-height: 72rpx;
				border-radius: 36rpx;
				text-align: center;
				display: block;
			}
		}

		>.dis {
			background-color: #bababa !important;
			color: #fff !important;
		}
	}

	// 购物车列表
	.shopCarBox {
		width: 100%;
		background-color: rgba(0, 0, 0, .5);
		position: fixed;
		z-index: 2;
		bottom: 153rpx;
		top: 0;

		overflow: hidden;
		.u-empty {
			min-height: 25vh;
		}

		.shopCarContent {
			padding: 20rpx;
			background-color: #fff;
			border-bottom: 1px solid #eee;
			border-top-left-radius: 20rpx;
			border-top-right-radius: 20rpx;
			overflow-y: auto;
			position: absolute;
			bottom: 0;
			width: calc(100% - 40rpx);
			max-height:calc(60vh + 80rpx);
			
			.foodList {
				margin: 40rpx 0;
				max-height:60vh;
				.foodItem{
					width: 100%;
					@include flexs(space-between,center);
					margin-bottom: 40rpx;
					&:last-child{
						margin-bottom:0;
					}

					.foodImage {
						width: 130rpx;
						height: 130rpx;
						margin-right: 20rpx;
						flex-shrink: 0;
						object-fit: cover;
						border-radius: 16rpx;
					}

					>view {
						flex: 1;
						min-height: 130rpx;
						@include flexs(space-between, flex-start);
						flex-direction: column;

						>view {
							@include flexs(space-between, center);
							width: 100%;
						}
						.foodName{
							font-size: 28rpx;
							font-weight: bold;
						}
						.specifications  {
							font-size: 24rpx;
							margin:12rpx 0;
							color: #aaa;
						
						}
						.foodPrice{
							font-size: 32rpx;
							color:#FF0404;
						}
					}
				}
			}
		
		}

		/deep/.uni-checkbox-input {
			border-radius: 50%;
			width: 40rpx;
			height: 40rpx;
		}

		/deep/.uni-checkbox-input-checked {
			border-color: #f1b825 !important;

			&:before {
				content: '';
				width: 35rpx;
				height: 35rpx;
				background-color: #f1b825;
				border-radius: 50%;
				display: block;
			}
		}
	}
</style>